package edu.tum.cs.stageflow.language.ui.views.schedule;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

public class ScheduleView extends ViewPart {

	// private ScheduleTable schedulePainter;
	private TableViewer scheduleTable;
	private Job getCurrentStageFlowStatusJob;
	private static final String DIR_LIVE_VIEW = "C:/StageFlowTestDir/liveSchedule/";

	@Override
	public void createPartControl(Composite parent) {
		scheduleTable = new TableViewer(parent, SWT.DOUBLE_BUFFERED
				| SWT.VIRTUAL);
		scheduleTable.setContentProvider(new ScheduleContentProvider());
		scheduleTable.setLabelProvider(new ScheduleLabelProvider());
		scheduleTable.setSorter(new ScheduleViewerSorter());

		startGetStageFlowStatusJob();
	}

	private void startGetStageFlowStatusJob() {
		getCurrentStageFlowStatusJob = new Job("Stage Flow State") {
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				monitor.beginTask("Get Stage Flow State",
						IProgressMonitor.UNKNOWN);
				for (File root : (new File(DIR_LIVE_VIEW)).listFiles()) {
					setBenchmarkResults(monitor, root);
				}
				if (monitor.isCanceled()) {
					return Status.CANCEL_STATUS;
				}
				return Status.OK_STATUS;
			}

		};
		getCurrentStageFlowStatusJob.setPriority(Job.DECORATE);
		getCurrentStageFlowStatusJob.schedule();

	}

	@Override
	public void dispose() {
		getCurrentStageFlowStatusJob.cancel();
		super.dispose();
	}

	@Override
	public void init(IViewSite site) throws PartInitException {
		// TODO Auto-generated method stub
		super.init(site);
	}

	@Override
	public void setFocus() {

	}

	private void setBenchmarkResults(IProgressMonitor monitor,
			File benchmarkFile) {
		BufferedReader br = null;
		String line = "";
		String cvsSplitBy = ";";
		Table table = scheduleTable.getTable();
		// throws -
		// if (parent.isDisposed()) error (SWT.ERROR_INVALID_ARGUMENT);
		// - in Widget.class
		TableColumn tc = new TableColumn(table, SWT.LEFT);
		tc.setText(benchmarkFile.getName());

		try {

			br = new BufferedReader(new FileReader(benchmarkFile));
			while ((line = br.readLine()) != null) {
				String[] row = line.split(cvsSplitBy);
				Long dur = Long.valueOf(row[1]);
				dur -= Long.valueOf(row[0]);
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	class ScheduleViewerSorter extends ViewerSorter {

		@Override
		public int compare(Viewer viewer, Object e1, Object e2) {
			int rc = 0;

			return rc;
		}
	}

	class ScheduleLabelProvider implements ITableLabelProvider {

		@Override
		public String getColumnText(Object element, int columnIndex) {
			return null;
		}

		@Override
		public void addListener(ILabelProviderListener listener) {
			// Ignore it
		}

		@Override
		public void dispose() {
			// Nothing to dispose
		}

		@Override
		public boolean isLabelProperty(Object element, String property) {
			return false;
		}

		@Override
		public void removeListener(ILabelProviderListener listener) {
			// Ignore
		}

		@Override
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}
	}

	class ScheduleContentProvider implements IStructuredContentProvider {

		@Override
		public Object[] getElements(Object arg0) {
			// Returns all the players in the specified team
			return new Object[0];
		}

		@Override
		public void dispose() {
		}

		@Override
		public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
			// Nothing to do
		}
	}
}
